package com.chidopi.market.bookstore.service.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.chidopi.cdf.dao.IBaseDao;
import com.chidopi.market.bookstore.dao.IBookItemsDao;
import com.chidopi.market.bookstore.domain.BookCategories;
import com.chidopi.market.bookstore.domain.BookItems;
import com.chidopi.market.bookstore.domain.BookLang;
import com.chidopi.market.bookstore.service.IBookItemsService;


public class BookItemsServiceImpl implements IBookItemsService {
	
	private static Logger logger = LoggerFactory.getLogger(BookItemsServiceImpl.class);
	
	@Autowired
	@Qualifier("bookItemsDao")
	private IBookItemsDao baseDao;
	
	@Autowired
	@Qualifier("bookCategoriesDao")
	private IBaseDao<BookCategories> categoriesDao;
	
	@Override
	public int insert(BookItems t) throws Exception {
		return this.baseDao.insert(t);
	}

	@Override
	public int update(BookItems t) throws Exception {

		return this.baseDao.update(t);
	}

	@Override
	public int delete(BookItems t) throws Exception {	
		return this.baseDao.delete(t);		
	}

	@Override
	public List<BookItems> find(String sqlWhere, Object... args) throws Exception {
		logger.debug(sqlWhere);
		logger.debug(Arrays.asList(args).toString());
		return this.baseDao.find(sqlWhere, args);
	}

	@Override
	public BookItems findById(Object id) throws Exception {
		
		BookItems book = this.baseDao.findById(id);
		
		if (null != book){
			// BookCategories
			String sql = "id in (select CATEGORY_ID from book2category where BOOK_ID = ? ) ";
			List<BookCategories> categories = categoriesDao.find(sql, id);
			book.setCategories(categories);
			// Album
			if(book.getAlbum_flag().equals("Y")){
				sql = " id in (select BOOK_ID from book2album where ALBUM_ID = ? )";
				List<BookItems> books = this.baseDao.find(sql, id);
				book.setBooks(books);
			}			
			// Langs	
			sql = "select * from booklang where BOOK_ID = ? ";
			List<BookLang> langs = this.baseDao.findBookLangs(sql,id);
			book.setBookLangs(langs);			
		}
		
		return book;
	}

	@Override
	public int getNextSequence() throws Exception {
		return 0;
	}

	@Override
	public List<BookCategories> findAllCategories() {
		
		return categoriesDao.find("");
	}

	@Override
	public List<BookItems> findAllSingleBooks() {
		String sql = "ALBUM_FLAG='N' AND ENABLE_FLAG='Y'";
		return baseDao.find(sql);
	}

	@Override
	public BookItems findSingleBookItemById(Object id) {
	    return this.baseDao.findById(id);		
	}

	@Override
	public List<String> getBookItemsBackupZipById(Object id) {
		
		return baseDao.findBackupZipById(id);
		
	}

//	@Override
//	public byte[] findIconImageById(Object id) {
//		return baseDao.findIconImageById(id);
//	}
//
//	@Override
//	public byte[] findThumbImageById(Object id) {
//		return baseDao.findThumbImageById(id);		
//	}

}
